import { gettoken } from '@/utils/storage';

const initialState = {
  profile: {} as ProfileType,
  auth: gettoken(),
  user: {} as userInfoType,
};
export type AuthType = {
  token: string;
  refresh_token: string;
};
export type userInfoType = {
  art_count: string;
  fans_count: string;
  follow_count: string;
  id: string;
  like_count: string;
  name: string;
  photo: string;
};
export type ProfileType = {
  id: string;
  photo: string;
  name: string;
  mobile: string;
  gender: number;
  birthday: string;
  // 💥 intro自己补上
  intro: string;
};
type userAtion =
  | { type: 'user/saveAuth'; payload: AuthType }
  | { type: 'user/saveUser'; payload: userInfoType }
  | { type: 'user/saveProfile'; payload: ProfileType }
  | { type: 'user/updateProfile'; payload: Partial<ProfileType> };
export default function userReducer(
  state = initialState,
  action: userAtion
): typeof initialState {
  switch (action.type) {
    case 'user/saveAuth':
      return {
        ...state,
        auth: action.payload,
      };
    case 'user/saveUser':
      return {
        ...state,
        user: { ...action.payload },
      };
    // 3. 编写保存数据的逻辑分支
    case 'user/saveProfile':
      return {
        ...state,
        profile: { ...action.payload },
      };
    // 2. 编写更新保存profile的逻辑
    case 'user/updateProfile':
      return {
        ...state,
        profile: {
          ...state.profile,
          ...action.payload,
        },
      };

    default:
      return state;
  }
}
